//
//  ViewController.m
//  UI_16_UICollectionView
//
//  Created by 李劲成 on 16/3/30.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "ViewController.h"
#import "CollectionOfFirst.h"
#import "CellForFirst.h"
#import "LayoutOfFirst.h"
static NSString *pool = @"pool1";
static NSString *header = @"header";
static NSString *footer = @"footer";
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)CollectionOfFirst *collection;

@property (nonatomic, retain)LayoutOfFirst *layoutOfFirst;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createLayout];
    [self createCollectionView];
  
    // Do any additional setup after loading the view, typically from a nib.
    
}

#pragma mark - 创建视图
- (void)createLayout {
    
    self.layoutOfFirst = [[LayoutOfFirst alloc]init];
}


#pragma mark - 创建 collectionView 
- (void)createCollectionView {
    // 初始化
    self.collection = [[CollectionOfFirst alloc]initWithFrame:BOUNDS collectionViewLayout:self.layoutOfFirst];
     // 注册cell(collectionView只支持先注册,再取出).
    [self.collection registerClass:[CellForFirst class] forCellWithReuseIdentifier:pool];
    // collectionView 的分区头部尾部视图采用重用池机制.
    // 头视图
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
    // 尾视图
    [self.collection registerClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footer];
    // 添加视图
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor greenColor];
    self.collection.showsVerticalScrollIndicator = NO;
    self.collection.bounces = NO;
    // 代理
    self.collection.delegate = self;
    self.collection.dataSource = self;
    // 添加手势.
    [self addGesture];
    
}

#pragma mark - 知识点3 Collect的Item实现移动.
/** collectionView上面加上长按手势. */
- (void)addGesture {
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleMove:)];
    [self.collection addGestureRecognizer:longPressGesture];
}

// 手势事件
- (void)handleMove:(UILongPressGestureRecognizer *)longPressGesture {
    // 根据长按手势的状态, 调用item的移动方法.
    switch (longPressGesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            // 根据手势在View上的点, 找到对应item的indexpath
            NSIndexPath *indexpath = [self.collection indexPathForItemAtPoint:[longPressGesture locationInView:self.collection]];
            // 如果没有点中item, 直接退出.
            if (indexpath == nil) {
                return;
            }
            else {
                [self.collection beginInteractiveMovementForItemAtIndexPath:indexpath];
            }
        }
            break;
        // 更新集合视图中的范围内的项的位置。
        case UIGestureRecognizerStateChanged:
        {
            // 随着手势移动, item位置不断更新.
            [self.collection updateInteractiveMovementTargetPosition:[longPressGesture locationInView:self.collection]];
        }
            break;
       // 结束交互式运动跟踪并将目标项目移动到它的新位置。
        case UIGestureRecognizerStateEnded: {
            [self.collection endInteractiveMovement];
        }
            break;
        default:
            [self.collection cancelInteractiveMovement];
            break;
    }
}

#pragma mark - CollectionView dataSource

#warning 1
/** 两个必须实现的协议方法(1/2) */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

/** ...协议方法(2/2) */
#warning 2
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:pool forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor blueColor];
    return cell;
}

/** 哪些item 可以移动. */
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

/** 返回每个分区的头部和尾部重用视图. */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    // 头视图
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header forIndexPath:indexPath];
        
        headerView.backgroundColor = [UIColor blueColor];
        return headerView;
    }
    // 尾视图
    else {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footer forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor blueColor];
        return footerView;
    }
}

/** 移动item调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    // 更新数据源!!!
    
}

#pragma mark - CollectionView Delegate
/** 选中item之后,调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.item);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
